package net.adetech.crj.job;

import net.adetech.crj.dao.HallMapper;
import net.adetech.crj.dao.QueueMapper;
import net.adetech.crj.entity.Queue;
import net.adetech.crj.utils.InnerServiceUtil;
import net.adetech.framework.entity.ServiceResponse;
import net.adetech.framework.utils.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class SyncQueueJob {

    @Autowired
    QueueMapper queueMapper;

    @Autowired
    HallMapper hallMapper;
    @Autowired
    InnerServiceUtil serviceUtil;

    @Value("${syncJob}")
    boolean syncJob;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void syncQueue() {
        if (!syncJob)
        {
            return;
        }
        //获取最大更新时间
        Timestamp maxUpdateTime = queueMapper.getMaxUpdateTime();
        if (maxUpdateTime == null) {
            try {
                maxUpdateTime = new Timestamp(new SimpleDateFormat("yyyyMMdd").parse("20180507").getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        ServiceResponse newQueueListResponse = serviceUtil.callService("queue.getNewQueueList", "maxUpdateTime", maxUpdateTime);
        List<Queue> queueList = (List<Queue>) newQueueListResponse.get("queueList");
        for (Queue q : queueList
                ) {
            Queue tempQueue = queueMapper.getQueueById(q.getQueueId());
            if (tempQueue==null)
            {
                q.setIdentityNumber(SerializeUtil.decodeBase64(q.getIdentityNumber()));
                q.setRealName(SerializeUtil.decodeBase64(q.getRealName()));
                q.setWxName(SerializeUtil.decodeBase64(q.getWxName()));
                queueMapper.insert(q);
            }
            else {
                queueMapper.update(q);
            }

        }


        String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());

        List<Queue> allList = queueMapper.getQueueListByDayId(dayId);
        int countA=0,countB=0,countC=0;
        if (allList!=null)
        {
            for (Queue q:allList)
            {
                if ("1".equals(q.getState())||"2".equals(q.getState()))
                {
                    if ("A".equals(q.getServiceZone()))
                    {
                        countA++;
                    }
                    if ("B".equals(q.getServiceZone()))
                    {
                        countB++;
                    }
                    if ("C".equals(q.getServiceZone()))
                    {
                        countC++;
                    }
                }
            }
        }
        hallMapper.updateQueueCount(countA,countB,countC);

    }
}
